import React, { Component } from 'react'
import { Layout,Menu,Button } from 'antd';
import request from '../../util/request';
import {Switch,Route,NavLink,Redirect} from 'react-router-dom'
import TeacherIndex from './teacherIndex'
import StudentIndex from './studentIndex'
import StudentManger from './studentManger'
import Person from './person'
import scoreManger from './scoreManger'
import IsLogin from '../../util/isLogin'

const { Header, Sider, Content } = Layout;


class Main extends Component {
    componentDidMount(){
        //获取菜单的接口
        request.get('/api/menuList').then(res => {
            console.log(res);
            if(res.code === 1){
                this.setState({
                    menuList:res.data
                })
            }
        })
    }
    state = {
        menuList:[],  //菜单列表
        username:localStorage.getItem('username'),
        role_name:localStorage.getItem('role_name')
    }
    render() {
        let {menuList,username,role_name} = this.state;
        return (
            <Layout className="wrap">
                <Header>
                    {username}-{role_name}
                    <Button onClick={this.exit}>退出</Button>
                </Header>
                <Layout>
                    <Sider>
                        <Menu theme="dark" mode="inline" defaultSelectedKeys={['1']}>
                            {menuList.length && menuList.map((item,index) => <Menu.Item key={index+1}>
                            <span><NavLink to={'/main'+item.menu_router}>{item.menu_name}</NavLink></span>
                            </Menu.Item>)}
                        </Menu>
                    </Sider>
                    <Content>
                        <Switch>
                            <Route path="/main/teacherIndex" component={TeacherIndex}></Route>
                            <Route path="/main/studentIndex" component={StudentIndex}></Route>
                            <Route path="/main/studentManger" component={StudentManger}></Route>
                            <Route path="/main/person" component={Person}></Route>
                            <Route path="/main/scoreManger" component={scoreManger}></Route>
                            <Redirect from="/main" to={role_name==='讲师'?'/main/teacherIndex':'/main/studentIndex'}></Redirect>
                        </Switch>
                    </Content>
                </Layout>
            </Layout>
        )
    }
    //退出
    exit = () => {
        //1.清空所有的本地存储
        localStorage.clear(); 
        //2.跳回登录
        this.props.history.push('/login');
    }
}

export default IsLogin(Main);